package com.gitee.elead.model.serializer;

import java.io.IOException;

import org.apache.commons.lang3.StringUtils;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.gitee.elead.util.ApplicationUtils;

import lombok.extern.slf4j.Slf4j;

/**
 * 接受字符串类型用户的时间转成用户ID
 * 
 * @author luopeng
 *
 */
@Slf4j
public class UserJsonDeserializer extends JsonDeserializer<String> {

	private IUserJsonDeserializerService userJsonDeserializerService;

	@Override
	public String deserialize(JsonParser jsonParser, DeserializationContext ctxt)
			throws IOException, JsonProcessingException {
		try {
			if (ObjectUtils.isNotEmpty(jsonParser) && StringUtils.isNotEmpty(jsonParser.getText())) {
				String user = jsonParser.getText();
				if (ObjectUtils.isEmpty(user)) {
					return null;
				}
				userJsonDeserializerService = ApplicationUtils.getBean(IUserJsonDeserializerService.class);
				if (ObjectUtils.isEmpty(userJsonDeserializerService)) {
					log.warn("IUserJsonDeserializerService is empty...");
					return null;
				}
				return userJsonDeserializerService.getUserId(user);
			} else {
				return null;
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

}
